/**
 * 微信相关的业务类
 *
 * @author lsquan
 */
package com.easyCms.wechat.busi;

import com.easyCms.base.common.CodeConst;
import com.easyCms.base.service.CmBannerSV;
import com.easyCms.base.utils.HttpClientUtils;
import com.easyCms.base.utils.JsonUtils;
import com.easyCms.wechat.busi.vo.ParseOpenIdVO;
import com.easyCms.wechat.busi.vo.WeChatUserInfoVO;
import com.easyCms.wechat.entity.WechatUser;
import com.easyCms.wechat.service.WechatUserSV;
import com.vdurmont.emoji.EmojiParser;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Transactional
@Component
public class WeChatApiBusi {
    private static final Logger logger = LoggerFactory.getLogger(WeChatApiBusi.class);

    /**
     * 存储用户信息
     *
     * @param weChatUserInfoVO
     */

    @Autowired
    private WechatUserSV wechatUserSV;
    @Autowired
    private CmBannerSV cmBannerSV;


    public WechatUser saveCrmUser(WeChatUserInfoVO weChatUserInfoVO) {
        String getOpenIdUrl = "https://api.weixin.qq.com/sns/jscode2session";
        String appid = "wx0b5d6e6cb69d05b7";
        String secret = "6c0dc4d58e71b8524d195809fb08cc06";
        String js_code = weChatUserInfoVO.getCode();
        String grant_type = "authorization_code";

        String requestData = "?appid=" + appid + "&secret=" + secret + "&grant_type" + grant_type + "&js_code="
                + js_code;
        String responseStr = null;
        ParseOpenIdVO openIdVO = null;
        try {
            responseStr = HttpClientUtils.get(getOpenIdUrl + requestData);
            if (StringUtils.isBlank(responseStr)) {
                return null;
            }
            openIdVO = (ParseOpenIdVO) JsonUtils.fromJson(responseStr, ParseOpenIdVO.class);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return null;
        }

        String openid = openIdVO.getOpenid();
        if (StringUtils.isBlank(openid)) {
            return null;
        }
        try {
            // 1.前台数据转换
            Date nowTime = new Date();

            // 2.查询用户存在性
            WechatUser wechatUserOrg = wechatUserSV.findValidByOpenId(openid);
            // 3.存在和 更新逻辑
            if (wechatUserOrg == null) { // 更新用户信息
                wechatUserOrg = new WechatUser();
                wechatUserOrg.setOpenId(openid);
                wechatUserOrg.setCreateTime(nowTime);
            }

            wechatUserOrg.setNickName(EmojiParser.removeAllEmojis(weChatUserInfoVO.getNickName()));
            wechatUserOrg.setAvatarUrl(weChatUserInfoVO.getAvatarUrl());
            wechatUserOrg.setCity(weChatUserInfoVO.getCity());
            wechatUserOrg.setCountry(weChatUserInfoVO.getCountry());
            wechatUserOrg.setLanguage(weChatUserInfoVO.getLanguage());
            wechatUserOrg.setSex(weChatUserInfoVO.getGender());
            wechatUserOrg.setState(CodeConst.State.STATE_VALID);
            WechatUser user = wechatUserSV.save(wechatUserOrg);
            return user;
        } catch (Exception e) {
            logger.error("微信用户登录创建信息异常" + e.getMessage(), e);
        }
        return null;
    }

}
